
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED


const double balanceR = 9700.0; //参考电阻阻值，越精确越好
const double ADC_max = 1024.0; 
/*使用beta方程计算阻值。*/ 
const double beta = 3950.0; //商家给出的电阻对应25°C下的bata值
const double roomTemp = 298.15; //以开尔文为单位的室温25°C
const double roomTempR = 10000.0; //NTC热敏电阻在室温25°C下具有典型的电阻
const double KelvinTemp = 273.15; //开文温度

int sensorValue = 0;  // variable to store the value coming from the sensor
int Adcmv = 0.0;
int Rec = 0.0;
float temperature = 0.0;
void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  
  Serial.begin (9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(500);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(500);
  
  Rec = balanceR/((ADC_max/sensorValue)-1);
  Serial.printf("R:%d,adc:%d\n",Rec,sensorValue);

  temperature =(beta * roomTemp)/(beta +(roomTemp * log(Rec / roomTempR)));  
  temperature = temperature  -  KelvinTemp; //将开尔文转换为摄氏温度
  
  Serial.printf("T:%.2f°C\n\n",temperature);
  
  //Serial.println(sensorValue,Adcmv); 
//  Serial.printf("adc:%d,mV:%d,R:%d,temp:%d %d\n\n",sensorValue,Adcmv,Rec,temperature,tem);
 

  
}
